
package com.zhonghong.entrance;

import com.zhonghong.excel.ExcelUtil;
import com.zhonghong.xml.XmlUtil;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 读取 strings.xml中的所有key值并生成写入到Excel文件
 *
 * @author zhonghong.zhangxg
 * @date 2016年7月25日下午5:03:43
 */
public class Xml2Xls implements ActionListener {
    private JFrame jf;
    private JPanel contentPane;
    private JButton xmlBtn;
    private JFileChooser fileChooser;
    private File xmlFile;
    
    private ExcelUtil excelUtil = new ExcelUtil();
    private XmlUtil xmlUtil = new XmlUtil();

    private static final String TITLE = "将strings.xml所有键值生成到Excel中";
    private static final String XML_BTN_STR = "选择XML文件";
    private static final int WIDTH = 300;
    private static final int HEIGHT = 100;

    /** 用户工作主目录 */
    private final static String PATH = System.getProperty("user.dir");

    public Xml2Xls() {
        jf = new JFrame(TITLE);
        jf.setSize(WIDTH, HEIGHT);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jf.setLocation(screenSize.width / 4, screenSize.height / 4);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setResizable(false);

        contentPane = new JPanel();
        jf.setContentPane(contentPane);

        xmlBtn = new JButton(XML_BTN_STR);

        contentPane.add(xmlBtn);

        initListener();
    }

    private void initListener() {
        xmlBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String eventFrom = e.getActionCommand();
        switch (eventFrom) {
            
            case XML_BTN_STR: {
                fileChooser = new JFileChooser(PATH);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.showDialog(new JLabel(), XML_BTN_STR);
                xmlFile = fileChooser.getSelectedFile();
                if (xmlFile == null) {
                    JOptionPane.showMessageDialog(new JButton("确定"), "未选择文件!!");
                    return;
                }
                if (checkFile(xmlFile, "xml")){
//                    ArrayList<String> keys = xmlUtil.readSrcXml(xmlFile);
//                    boolean result = excelUtil.createAndWriteExcel(PATH  + File.separator + "strings.xls", keys);
                    List<String>[] keyValues = xmlUtil.readXMlWithArray(xmlFile);
                    boolean result = excelUtil.writeKeyValue2Excel(PATH  + File.separator + "strings.xls", keyValues);
                    if (result) {
                        JOptionPane.showMessageDialog(new JButton("确定"), "生成文件路径为：" + PATH);
                    } else {
                        JOptionPane.showMessageDialog(new JButton("确定"), "失败!!!");
                    }
                }
                break;
            }
        }
    }
    
    
    /**
     * 检查文件格式是否正确
     * 
     * @param file: 要检查的文件
     * @param selfSuffix: 匹配的后缀,如 xls(不包含 . )
     * @return 校验合法返回true, 否则返回false
     */
    private boolean checkFile(File file, String selfSuffix) {
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.indexOf(".") + 1, fileName.length());
        if (!suffix.equalsIgnoreCase(selfSuffix)) { // 选择的文件类型不对
            JOptionPane.showMessageDialog(new JButton("确定"), "文件类型错误！");
            return false;
        } else {
            return true;
        }
    }
}
